package com.yiwenup.leetcode.site;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.site
 * @Author yiwenup
 * @Date 2024-12-23 22:09:19
 * @Description https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
 */
public class No1047 {
    public String removeDuplicates(String s) {
        Deque<Character> queue = new ArrayDeque<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (queue.isEmpty() || queue.peekLast() != c) {
                queue.offerLast(c);
            } else {
                queue.pollLast();
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!queue.isEmpty()) {
            sb.append(queue.pollFirst());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(new No1047().removeDuplicates("abbaca"));
    }
}
